﻿package org.cwnuit.train.acton;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.cwnuit.train.domain.Rules;
import org.cwnuit.train.domain.Trains;
import org.cwnuit.train.domain.Users;
import org.cwnuit.train.service.RulesService;
import org.cwnuit.train.service.TrainsService;
import org.cwnuit.train.utils.MapUtil;
import org.cwnuit.train.utils.PageModel;
import org.cwnuit.train.utils.Today;
import org.cwnuit.train.vo.SworkRuleVO;
@SuppressWarnings("all")
public class RulesAction extends HttpServlet {

	RulesService rulesServices = new RulesService();
	TrainsService trainsServices = new TrainsService();

	protected void service(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String cmd = request.getParameter("cmd");
		
		if (cmd.equals("list")) {// 分页列出信息
			int pid = Integer.parseInt(request.getParameter("pid"));
			request.setAttribute("pm", listRule(request));
			request.setAttribute("content_tittle", "规章制度列表");
			request.setAttribute("main", findRule(pid));
			request.getRequestDispatcher("web/rules/list.jsp").forward(request,response);
		} else if (cmd.equals("adding")) {//添加前准备数据信息
			request.setAttribute("content_tittle", "录入《站细》编制规则及参考规章条款");
			request.setAttribute("pid", request.getParameter("pid"));
			request.getRequestDispatcher("web/rules/input_com.jsp").forward(request, response);
		} else if (cmd.equals("adding_main")) {//添加目录前准备数据信息
			request.setAttribute("content_tittle", "添加新的目录,级别【篇】");
			request.setAttribute("rlevel", "1");
			request.getRequestDispatcher("web/rules/input_nav.jsp").forward(request, response);
		} else if (cmd.equals("adding_nav")) {//添加目录前准备数据信息
			int rlevel = Integer.parseInt(request.getParameter("rlevel")) + 1;
			String rlevelStr = null;
			if (rlevel == 2) {
				rlevelStr = "章";
			} else if (rlevel == 3) {
				rlevelStr = "节";
			}
			request.setAttribute("content_tittle", "添加新的目录,级别【" + rlevelStr + "】");
			request.setAttribute("rlevel", rlevel);
			request.setAttribute("pid", request.getParameter("pid"));
			request.getRequestDispatcher("web/rules/input_nav.jsp").forward(request, response);
		} else if (cmd.equals("update")) {// 修改信息
			int rid = Integer.parseInt(request.getParameter("rid"));
			request.setAttribute("rule", findRule(rid));
			request.setAttribute("content_tittle", "修改站细编制规则及参考依据");
			request.getRequestDispatcher("web/rules/input_com.jsp").forward(request, response);
		} else if (cmd.equals("update_nav")) {// 修改目录
			int rid = Integer.parseInt(request.getParameter("rid"));
			Rules rule = findRule(rid);
			request.setAttribute("rule", rule);
			String rlevelStr = null;
			if (rule.getRlevel() == 1) {
				rlevelStr = "篇";
			} else if (rule.getRlevel() == 2) {
				rlevelStr = "章";
			} else if (rule.getRlevel() == 3) {
				rlevelStr = "节";
			}
			request.setAttribute("content_tittle", "修改目录,级别【" + rlevelStr + "】");
			request.getRequestDispatcher("web/rules/input_nav.jsp").forward(request, response);
		} else if (cmd.equals("list_main")) {//废弃的
			request.setAttribute("main", list_main(request));
			request.setAttribute("content_tittle", "章节目录");
			request.getRequestDispatcher("web/rules/list_main.jsp").forward(request,response);
		} else if (cmd.equals("input_nav")) {// 添加或者修改目录
			if (request.getParameter("rid") == null || request.getParameter("rid").equals("")) {
				if (addNav(request)) {
					request.getSession().setAttribute("msg", "恭喜你，你已经成功添加了一个新目录！");
				} else {
					request.getSession().setAttribute("msg", "对不起，你添加目录失败或者已经存在！");
				}
			} else {
				if (updateNav(request)) {
					request.getSession().setAttribute("msg", "恭喜你，你已经成功修改了目录！");
				} else {
					request.getSession().setAttribute("msg", "对不起，你修改目录失败！");
				}
			}
			response.sendRedirect("common?cmd=listtree");
		} else if (cmd.equals("input")) {// 添加或者修改
			if (request.getParameter("rid") == null || request.getParameter("rid").equals("")) {
				if (addRule(request)) {
					request.getSession().setAttribute("msg", "恭喜你，你已经成功添加了一条制度！");
				} else {
					request.getSession().setAttribute("msg", "对不起，你添加制度失败或者已经存在！");
				}
				response.sendRedirect("rules?cmd=adding&pid=" + request.getParameter("pid")); 
			} else {
				if (updateRule(request)) {
					request.getSession().setAttribute("msg", "恭喜你，你已经成功修改了一条制度！");
				} else {
					request.getSession().setAttribute("msg", "对不起，你修改制度失败！");
				}
				response.sendRedirect("common?cmd=listtree");
			}
		} else if (cmd.equals("delete_nav")) {// 删除信息
			if (deleteRule(request)) {
				request.getSession().setAttribute("msg", "恭喜你，你已经成功删除了一条记录！");
			} else {
				request.getSession().setAttribute("msg", "对不起，你删除记录失败！");
			}
			response.sendRedirect("common?cmd=listtree");
		} else if (cmd.equals("delete")) {// 删除信息
			if (deleteRule(request)) {
				request.getSession().setAttribute("msg", "恭喜你，你已经成功删除了一条记录！");
			} else {
				request.getSession().setAttribute("msg", "对不起，你删除记录失败！该条目如果有下级你要先删除下面的条目。");
			}
			response.sendRedirect("common?cmd=listtree");
		} else if (cmd.equals("list_zt")) {// 编制组进入后列出编制状态
			request.setAttribute("pm", listRuleSwork(request));
			request.setAttribute("content_tittle", "规章编制状态");
			request.getRequestDispatcher("web/rules/list_zt.jsp").forward(request,response);
		} else if (cmd.equals("list_sh")) {// 审核组进入后列出审核状态
			int tid = Integer.parseInt(request.getParameter("tid"));
			List<Trains> trainslist = listTrain(request);
			request.setAttribute("trains", trainslist);
			if (tid == 0 ) {
				tid = trainslist.get(0).getTid();
			}
			request.setAttribute("number", tid);
			request.setAttribute("pm", listRuleSwork(request,tid));
			request.setAttribute("content_tittle", "规章审核状态");
			request.getRequestDispatcher("web/rules/list_sh.jsp").forward(request,response);
		}
		
	}
 
	// 分页查询规章信息
	private List<Rules> listRule(HttpServletRequest request) {
		String pid = request.getParameter("pid");
		String SQL = "SELECT * FROM Rules WHERE pid = '" + pid + "' ORDER BY rid ASC";
		return rulesServices.findRules(SQL);
	}
	
	// 车站信息
	private List<Trains> listTrain(HttpServletRequest request) {
		String SQL = "SELECT * FROM Trains WHERE tstate !=10000  ORDER BY tid ASC";
		return trainsServices.findTrains(SQL);
	}
	
	// 分页查询规章编制信息
	private PageModel listRuleSwork(HttpServletRequest request) {
		int offset = 0;
		try {
			offset = Integer.parseInt(request.getParameter("pager.offset"));
		} catch (NumberFormatException e) {
			offset = 0;
		}
		int tid = Integer.parseInt(request.getParameter("tid"));
		String SQL = "SELECT * FROM Rules WHERE rlevel = 4 ORDER BY rid ASC";
		return rulesServices.listRuleSwork(offset,10,SQL,tid);
	}
	
	// 分页查询规章编制信息
	private PageModel listRuleSwork(HttpServletRequest request,int tid) {
		int offset = 0;
		try {
			offset = Integer.parseInt(request.getParameter("pager.offset"));
		} catch (NumberFormatException e) {
			offset = 0;
		}
		String SQL = "SELECT * FROM Rules WHERE rlevel = 4 ORDER BY rid ASC";
		return rulesServices.listRuleSwork(offset,10,SQL,tid);
	}
	
	
	// 查找指定目录的信息
	private List<Rules> list_main(HttpServletRequest request) {
		String SQL = "SELECT * FROM rules WHERE rlevel = '1'";
		return rulesServices.findRules(SQL);
	}
	
	// 添加目录信息
	private boolean addNav(HttpServletRequest request) {
		String rname = request.getParameter("rname");
		String rlevel = request.getParameter("rlevel");
		String pid = request.getParameter("pid");
		
		String SQL = "INSERT INTO rules(rname,rlevel,pid,raddtime) VALUES('"
				+ rname + "','" + rlevel + "'," + pid + ",'"
				+ new Today().getAllDateOfNum() + "')";
		if (pid.equals("")||pid == null) {
			SQL = "INSERT INTO rules(rname,rlevel,raddtime) VALUES('"
				+ rname + "','" + rlevel + "','"
				+ new Today().getAllDateOfNum() + "')";
		} 
		return rulesServices.addRule(SQL);
	}

	// 修改目录信息
	private boolean updateNav(HttpServletRequest request) {
		int rid = Integer.parseInt(request.getParameter("rid"));
		String rname = request.getParameter("rname");
		String SQL = "UPDATE rules SET rname = '" + rname + "',rupdatetime = '"
				+ new Today().getAllDateOfNum() + "' WHERE rid = " + rid;
		return rulesServices.updateRule(SQL);
	}
	
	// 添加规章信息
	private boolean addRule(HttpServletRequest request) {
		int pid = Integer.parseInt(request.getParameter("pid"));
		String rname = request.getParameter("rname");
		String rcommon = request.getParameter("rcommon");
		String rtdbrule = request.getParameter("rtdbrule");
		String learntime = request.getParameter("learntime");
		if (learntime.equals("") || learntime == null) {
			learntime = "0";
		} 
		String SQL = "INSERT INTO rules(rname,rcommon,rtdbrule,pid,rlevel,learntime,raddtime) VALUES('"
				+ rname + "','" + rcommon + "','" + rtdbrule + "',"+ pid + ","+ 4 + ","+ learntime + ",'"
				+ new Today().getAllDateOfNum() + "')";
		return rulesServices.addRule(SQL);
	}

	// 删除规章信息
	private boolean deleteRule(HttpServletRequest request) {
		int rid = Integer.parseInt(request.getParameter("rid"));
		String SQL = "DELETE FROM rules WHERE rid = " + rid;
		return rulesServices.deleteRule(SQL);
	}

	// 修改规章信息
	private boolean updateRule(HttpServletRequest request) {
		int rid = Integer.parseInt(request.getParameter("rid"));
		String p = request.getParameter("p");
		String SQL = "";
		if (p.equals("com")) {
			String rtdbrule = request.getParameter("rtdbrule");
			String rname = request.getParameter("rname");
			String rcommon = request.getParameter("rcommon");
			String learntime = request.getParameter("learntime");
			SQL = "UPDATE rules SET rname = '" + rname + "',rcommon = '"
					+ rcommon + "', rtdbrule = '" + rtdbrule + "', learntime = " + learntime
					+ ",rupdatetime = '"
					+ new Today().getAllDateOfNum() + "' WHERE rid = " + rid;
		} else if (p.equals("part")) {
			String rtdbrule = request.getParameter("rtdbrule");
			SQL = "UPDATE rules SET rtdbrule = '" + rtdbrule
					+ "',rupdatetime = '" + new Today().getAllDateOfNum()
					+ "' WHERE rid = " + rid;
		} else if (p.equals("way")) {
			String rtljrule = request.getParameter("rtljrule");
			SQL = "UPDATE rules SET rtljrule = '" + rtljrule
					+ "',rupdatetime = '" + new Today().getAllDateOfNum()
					+ "' WHERE rid = " + rid;
		} else if (p.equals("road")) {
			String rcwdrule = request.getParameter("rcwdrule");
			SQL = "UPDATE rules SET rcwdrule = '" + rcwdrule
					+ "',rupdatetime = '" + new Today().getAllDateOfNum()
					+ "' WHERE rid = " + rid;
		}

		return rulesServices.updateRule(SQL);
	}
	
	// 查找规章信息
	private Rules findRule(int rid) {
		String SQL = "SELECT * FROM Rules WHERE rid = " + rid;
		return rulesServices.findRule(SQL);
	}
}
